package cn.edu.sdyu.ie.crs.api;

import cn.edu.sdyu.ie.crs.common.response.PageVO;
import cn.edu.sdyu.ie.crs.common.response.Result;
import cn.edu.sdyu.ie.crs.domain.Classroom;
import cn.edu.sdyu.ie.crs.domain.DeviceRunLog;
import cn.edu.sdyu.ie.crs.service.ClassroomService;
import cn.edu.sdyu.ie.crs.service.DeviceRunLogService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.v3.oas.annotations.Operation;
//import io.swagger.v3.oas.annotations.Parameter;
//import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

@Tag(name = "设备运行日志管理", description = "设备运行日志信息管理接口")
@Slf4j
@RestController
@RequestMapping("api/deviceRunLog")
public class DeviceRunLogController {

    @Autowired
    private DeviceRunLogService deviceRunLogService;

    /**
     * 添加新设备运行日志信息
     *
     * @param deviceRunLog
     * @return
     */
    @Operation(tags = "设备运行日志管理", summary = "添加设备运行日志信息")
    @PostMapping("")
    public Result<Void> addDeviceRunLog(DeviceRunLog deviceRunLog) {

        if (deviceRunLog.getId() != null && !deviceRunLog.getId().equals(0)) {
            log.warn("请求ID参数，若更新对象，请使用PUT方式发起请求。");
            return Result.failure();
        }
        deviceRunLogService.saveDeviceRunLog(deviceRunLog);
        return Result.success();
    }

    /**
     * 更新设备运行日志信息
     *
     * @param deviceRunLog
     * @return
     */
    @Operation(tags = "设备运行日志管理", summary = "修改设备运行日志信息")
    @PutMapping("")
    public Result<Object> updateDeviceRunLog(DeviceRunLog deviceRunLog) {

        if (deviceRunLog.getId() == null || deviceRunLog.getId().equals(0)) {
            log.warn("未请求ID参数，使用POST方式创建对象。当前不会更新任何对象");
            return Result.failure();
        }
        deviceRunLogService.saveDeviceRunLog(deviceRunLog);
        return Result.success();
    }

    /**
     * 删除指定ID的设备运行日志信息
     *
     * @param id
     * @return
     */
    @Operation(tags = "设备运行日志管理", summary = "删除设备运行日志信息")
    @DeleteMapping("")
    public Result<Void> deleteDeviceRunLogById(@RequestParam Integer id) {
        deviceRunLogService.deleteDeviceRunLogById(id);
        return Result.success();
    }

    /**
     * 查询指定ID的教室信息
     *
     * @param id
     * @return
     */
    @Operation(tags = "设备运行日志管理", summary = "查询指定ID的设备运行日志信息")
//    @Parameters(value = {
//            @Parameter(description = "设备运行日志ID")
//    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "设备运行日志ID")
    })
    @GetMapping("/{id}")
    public Result<DeviceRunLog> findDeviceRunLogById(/*@Parameter(description = "教室ID")*/ @PathVariable Integer id) {
        DeviceRunLog deviceRunLog = deviceRunLogService.findDeviceRunLogById(id);

        if (deviceRunLog == null) {
            return Result.failure();
        }

        return Result.success(deviceRunLog);
    }

    /**
     * 分页查询设备运行日志信息
     * @param page
     * @param pageSize
     * @return
     */
    @Operation(tags = "设备运行日志管理", summary = "分页查询设备运行日志信息")
    @GetMapping("")
    public Result<PageVO<DeviceRunLog>> listDeviceRunLogsPage(@RequestParam(defaultValue = "1") Integer page,
                                                        @RequestParam(defaultValue = "10") Integer pageSize) {

        Page<DeviceRunLog> list = deviceRunLogService.findByPage(page - 1, pageSize);
        PageVO<DeviceRunLog> pageVO = PageVO.getPageVO(list);
        return Result.success(pageVO);
    }
}

